package com.itts.Validator;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class LisPlanValidatorFactory implements ApplicationContextAware {

    public static Map<String, LisPlanValidator> map = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        Map<String, LisPlanValidator> beansOfType = applicationContext.getBeansOfType(LisPlanValidator.class);
//        beansOfType.forEach((name, clazz) -> map.putIfAbsent(clazz.planCode(), clazz));
        //2.0
        applicationContext.getBeansOfType(LisPlanValidator.class).values().forEach(s -> map.putIfAbsent(s.planCode(), s));
    }

    public static LisPlanValidator getValidator(String planCode) {
        return map.getOrDefault(planCode, map.get("default"));
    }

}
